//
//  ImagePickerManager.m
//  Simubang_iphone
//
//  Created by Kelly xu on 16/4/19.
//  Copyright © 2016年 Simubang. All rights reserved.
//

#import "ImagePickerManager.h"
#import "AppDelegate.h"
@interface ImagePickerManager(){
    
}
@property(nonatomic,assign)BOOL isEditEnabled;
@end
@implementation ImagePickerManager
@synthesize completion = _completion;
+(ImagePickerManager*)sharedImgPicker{
    static ImagePickerManager *_sharedImgPicker = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        _sharedImgPicker = [[ImagePickerManager alloc]init];
    });
    return _sharedImgPicker;
}
-(void)showCamaraWithCompletion:(imagePickerCallBack)completion{
    if ([self isCameraAvailable]) {
        self.camaraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        dispatch_async(dispatch_get_main_queue(), ^{
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self.camaraPicker animated:YES completion:nil];
        });
    }else{
        //            [DDWarning showMessage:@"您的设备没有相机"];
    }
    self.completion = completion;
}
-(void)showImgActionWithCompletion:(imagePickerCallBack)completion{
    [self showImgActionWithType:ImagePickerTypeAll enableEditing:YES Completion:completion];
}
-(void)showImgActionWithType:(enum ImagePickerType)imagePickerType enableEditing:(BOOL)enableEdting Completion:(imagePickerCallBack)completion {
    self.isEditEnabled = enableEdting;
    UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    @weakify(self);
    UIAlertAction *camaraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        if ([self isCameraAvailable]) {
            self.camaraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            dispatch_async(dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self.camaraPicker animated:YES completion:nil];
            });
        }else{
            Toast(@"请开启您的相机权限");
        }
    }];
    
    UIAlertAction *libraryAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        @strongify(self);
        if ([self isPhotoLibraryAvailable]) {
            self.camaraPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            dispatch_async(dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self.camaraPicker animated:YES completion:nil];
            });
        }else{
//            [DDWarning showMessage:@"您没有权限调用相册！"];
        }
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    switch (imagePickerType) {
        case ImagePickerTypeAll:
        {
            [alertCtrl addAction:camaraAction];
            [alertCtrl addAction:libraryAction];
        }
            break;
        case ImagePickerTypePhoto:
        {
            [alertCtrl addAction:libraryAction];
        }
            break;
        case ImagePickerTypeCamara:
        {
            [alertCtrl addAction:camaraAction];
        }
            break;
        default:
            break;
    }
    [alertCtrl addAction:cancelAction];
    dispatch_async(dispatch_get_main_queue(), ^{
       [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertCtrl animated:YES completion:nil];
    });
    self.completion = completion;
}

-(void)selectImgWithType:(enum ImagePickerType)imagePickerType Completion:(imagePickerCallBack)completion {
    switch (imagePickerType) {
        case ImagePickerTypeCamara:
        {
            if ([self isCameraAvailable]) {
                self.camaraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self.camaraPicker animated:YES completion:nil];
                });
            }else{
//                 [DDWarning showMessage:@"您的设备没有相机"];
            }
        }
            break;
        case ImagePickerTypePhoto:
        {
            if ([self isPhotoLibraryAvailable]) {
                self.camaraPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self.camaraPicker animated:YES completion:nil];
                });
            }else{
//                [DDWarning showMessage:@"您没有权限调用相册！"];
            }
        }
            break;
        default:
            break;
    }
    self.completion = completion;
}

-(UIImagePickerController*)camaraPicker{
    if (!_camaraPicker) {
        _camaraPicker = [[UIImagePickerController alloc]init];
        _camaraPicker.delegate = self;
        _camaraPicker.allowsEditing = self.isEditEnabled;
        _camaraPicker.navigationBar.tintColor = [UIColor blackColor];
        _camaraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    return _camaraPicker;
}
#pragma mark - UIImagePickerControllerDelegate 代理方法
// 判断设备是否有摄像头
- (BOOL) isCameraAvailable{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
// 判断设备是否有摄像头
- (BOOL) isPhotoLibraryAvailable{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
}
// 保存图片后到相册后，调用的相关方法，查看是否保存成功
- (void) imageWasSavedSuccessfully:(UIImage *)paramImage didFinishSavingWithError:(NSError *)paramError contextInfo:(void *)paramContextInfo{
    if (paramError == nil){
        NSLog(@"Image was saved successfully.");
    } else {
        NSLog(@"An error happened while saving the image.");
        NSLog(@"Error = %@", paramError);
    }
}

// 当得到照片或者视频后，调用该方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSLog(@"Picker returned successfully.");
    NSLog(@"%@", info);
    UIImage *theImage = nil;
    // 判断，图片是否允许修改
    if ([picker allowsEditing]){
        //获取用户编辑之后的图像
        theImage = [info objectForKey:UIImagePickerControllerEditedImage];
    } else {
        // 照片的元数据参数
        theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        
    }
    self.completion(theImage,picker);
    
}
// 当用户取消时，调用该方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end
